<?php

//echo 1;
////echo phpinfo();
//$con = mysqli_connect("localhost","root","root");
//if (!$con)
//{
//    die('Could not connect: ' . mysqli_error());
//} else {
//    echo 'Db ok!';
//}
//// some code
//mysqli_close($con);

$con = mysqli_connect("localhost","root","root");
if (!$con)
{
    die('连接失败: ' . mysqli_error($con));
}else{
    echo "连接成功<br>";
}
//if (mysqli_query($con,"CREATE DATABASE PHPProjects"))
//{
//    echo "Database created";
//}
//else
//{
//    echo "创建失败: " . mysqli_error($con);
//}

// Create table in my_db database
if (mysqli_select_db($con,"PHPProjects")){
    echo "连接数据库成功<br>";
}else{
    echo "连接数据库失败<br>";
}
//$sql = "CREATE TABLE comment
//(
//uname varchar(15),
//comment varchar(255)
//)";
//if (mysqli_query($con,$sql)){
//    echo "创建表成功<br>";
//}else{
//    echo "创建表失败<br>";
//}

//mysqli_query($con,"INSERT INTO comment (id,uname, comment)
//VALUES ('1', '张三', '哈哈哈')");
//mysqli_query($con,"INSERT INTO comment (id, uname, comment)
//VALUES ('2', '李四', '嘻嘻嘻')");

$sql="INSERT INTO comment (uname, comment)
VALUES
('$_POST[uname]','$_POST[comment]')";
if (!mysqli_query($con,$sql))
{
    die('添加错误: ' . mysqli_error($con));
}
echo "添加成功";

$result = mysqli_query($con,"SELECT * FROM comment");
echo "<table border='1'>
<tr>
<th>名字</th>
<th>评论</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['uname'] . "</td>";
    echo "<td>" . $row['comment'] . "</td>";
    echo "</tr>";
}
echo "</table>";

//mysqli_query($con,"UPDATE comment SET uname = '赵六',comment = 'xxhh'
//WHERE id = '2'");

//mysqli_query($con,"DELETE FROM comment WHERE uname='赵六'");

mysqli_close($con);






